package hc.uco

class HistoriaPregressaController extends BaseController {

	def beforeInterceptor = [action:this.&auth ]
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [historiaPregressaInstanceList: HistoriaPregressa.list(params), historiaPregressaInstanceTotal: HistoriaPregressa.count()]
    }

    def create = {
        def historiaPregressaInstance = new HistoriaPregressa()
        historiaPregressaInstance.properties = params
        return [historiaPregressaInstance: historiaPregressaInstance]
    }

    def save = {
        def historiaPregressaInstance = new HistoriaPregressa(params)
        if (historiaPregressaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), historiaPregressaInstance.id])}"
            redirect(action: "show", id: historiaPregressaInstance.id)
        }
        else {
            render(view: "create", model: [historiaPregressaInstance: historiaPregressaInstance])
        }
    }

    def show = {
        def historiaPregressaInstance = HistoriaPregressa.get(params.id)
        if (!historiaPregressaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
            redirect(action: "list")
        }
        else {
            [historiaPregressaInstance: historiaPregressaInstance]
        }
    }

    def edit = {
        def historiaPregressaInstance = HistoriaPregressa.get(params.id)
        if (!historiaPregressaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [historiaPregressaInstance: historiaPregressaInstance]
        }
    }

    def update = {
		def historiaPregressaInstance = new HistoriaPregressa()
		if (params.id) {
			System.out.println("update")
	        historiaPregressaInstance = HistoriaPregressa.get(params.id.toLong())
	        if (historiaPregressaInstance) {
	            if (params.version) {
	                def version = params.version.toLong()
	                if (historiaPregressaInstance.version > version) {
	                    historiaPregressaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa')] as Object[], "Another user has updated this HistoriaPregressa while you were editing")
	                    //render(view: "edit", model: [historiaPregressaInstance: historiaPregressaInstance])
	                    //return
	                }
	            }
	            historiaPregressaInstance.properties = params
				historiaPregressaInstance.updatedBy = session.usuario;
	            if (!historiaPregressaInstance.hasErrors() && historiaPregressaInstance.save(flush: true)) {
	                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), historiaPregressaInstance.id])}"
	                //redirect(action: "show", id: historiaPregressaInstance.id)
	            }
	            //else {
	              //  chain(view: "manage", id: params.paciente.id, model: [historiaPregressa: historiaPregressaInstance, 'pacienteInstance':Paciente.get(params.paciente.id)])
	            //}
	        }
		}
        else {
			System.out.println("insert")
			params['id'] = null
            //flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
            historiaPregressaInstance = new HistoriaPregressa(params)
			historiaPregressaInstance.createdBy = session.usuario;
			historiaPregressaInstance.updatedBy = historiaPregressaInstance.createdBy;
	        if (historiaPregressaInstance.save(flush: true)) {
	            flash.message = "${message(code: 'default.created.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), historiaPregressaInstance.id])}"
	        } else
				System.out.println("failed")
        }
		chain(action: "manage", id: params.paciente.id, model: [ historiaPregressa: historiaPregressaInstance])
    }

    def delete = {
        def historiaPregressaInstance = HistoriaPregressa.get(params.id)
        if (historiaPregressaInstance) {
            try {
                historiaPregressaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'historiaPregressa.label', default: 'HistoriaPregressa'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def manage = {
		def historiaPregressaInstance = new HistoriaPregressa();
		historiaPregressaInstance.properties = params;
		
		if (request.method == 'GET') {
			def paciente = Paciente.get(params.id)
			params['paciente.id'] = params.id
			def hp = HistoriaPregressa.findByPaciente(paciente);
			if (chainModel) {
				def model = chainModel["historiaPregressa"]
				historiaPregressaInstance = model
				if (hp) {
					historiaPregressaInstance.createdBy = hp.createdBy
					historiaPregressaInstance.dateCreated = hp.dateCreated
				}
			} else {
				historiaPregressaInstance = hp
			}
			
			if (!historiaPregressaInstance) {
				historiaPregressaInstance = new HistoriaPregressa();
			}
			
			['pacienteInstance':paciente, historiaPregressaInstance: historiaPregressaInstance]
		} 
	}
}
